﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;

using SBPweb.PROvibe.Shared.Library.Components;
using SBPweb.PROvibe.Shared.Library.Layout;

using SBPweb.PROvibe.Server.Core.Modularity;
using SBPweb.PROvibe.Server.Core.Services;

using SBPweb.PROvibe.Server.Library.Base;
using SBPweb.PROvibe.Server.Library.Contracts;
using SBPweb.PROvibe.Server.Library.Services;

using SBPweb.PROvibe.Server.Modules.UserProvider.Contracts;

namespace SBPweb.PROvibe.Server.Core.Services
{

    [ServiceBehavior(Name = "IContentContextProviderService", Namespace = Library.Constants.Services.CoreNamespace)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ContentContextProviderService : CoreServiceBase<IContentContextProviderService>, IContentContextProviderService
    {

        #region Constructor logic

        public ContentContextProviderService()
            : base()
        {
        }

        public ContentContextProviderService(IModule parentModule)
            : base(parentModule)
        {
        }

        #endregion

        #region Properties

        public override string Name
        {
            get
            {
                return "ContentContextProviderService";
            }
        }

        protected internal IConfigurationProviderService ConfigurationProvider
        {
            get
            {
                return ServerContext.Instance.ServiceManager.Services.FirstByType<IConfigurationProviderService>();
            }
        }

        #endregion

        #region Operations
        
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public PageContext GetCurrentPageContext(string path)
        {
            Suite suite = ConfigurationProvider.GetCurrentSuite();
            List<Module> modules = ConfigurationProvider.EnumerateClientModulesOfSuite(suite).Cast<Module>().ToList();
            Application app = new Application();
            Page page = new Page(app);
            page.Id = 666;
            page.Name = "Desktop";
            ManagableReadOnlyObservableCollection<IZone> zones = page.Zones as ManagableReadOnlyObservableCollection<IZone>;
            zones.ManagableItems.Add(new Zone()
            {
                Id = 999,
                Name = "ModuleBar"
            });

            return new PageContext(modules, app, page, zones.Cast<Zone>().ToList());
        }

        #endregion

    }

}